#include "StdAfx.h"
#include "DAC.h"

void CDAC::BuildTables()
{
int n;

for(n=0;n<256;n++)
	{
	VolumeTable_Unsigned[n] = (256 * n) / 2;
	VolumeTable_Signed[n] = (256 * n) - 0x8000;
	}
}

CDAC::CDAC()
{
BuildTables();
message("dac initialized\n");
}

CDAC::~CDAC()
{
}

void CDAC::Reset()
{
int i;

for(i=0;i<4;i++)
	CurOutput[i] = 0;
BufStart = 0;
BufOffset = 0;
}

void CDAC::Write(u32 n,u8 data)
{
int output = VolumeTable_Unsigned[data];

CurOutput[n] = output;
}

void CDAC::WriteSigned(u32 n,u8 data)
{
int output = VolumeTable_Signed[data];

CurOutput[n] = output;
}

void CDAC::Update(u32 n,s16 *buf,int len)
{
int i,j,v;

for(i=0,j=BufStart;i<len;i++,j++)
	{
	v = (j + BufOffset) % DAC_BUF_LEN;
	buf[i] = Buffer[v];
	}
BufStart = (BufStart + BufOffset) % DAC_BUF_LEN;
BufOffset = 0;
}

void CDAC::UpdateSample()
{
int n = (BufStart + BufOffset) % DAC_BUF_LEN;

Buffer[n] = CurOutput[0];
BufOffset++;
}

void CDAC::UpdateSamples(int n)
{
for(;n;n--)
	UpdateSample();
}
